import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import BaseColor from '@main/entities/service/admin/base/base-color.entity';
import { ApiException } from '@main/common/exceptions/api.exception';
import { SysOperationLogService } from '../../system/operationLog/operationLog.service';

@Injectable()
export class BaseColorService {
  constructor(
    @InjectRepository(BaseColor)
    private colorRepository: Repository<BaseColor>,
    private operationLogService: SysOperationLogService
  ) {}

  async list() {
    return await this.colorRepository.find();
  }

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { colorName, colorCode, colorModel } = entity;

    return await this.colorRepository.findAndCount({
      where: {
        colorName: Like(`%${colorName}%`),
        colorCode: Like(`%${colorCode}%`),
        colorModel: Like(`%${colorModel}%`),
      },
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const color = await this.colorRepository.insert({
      ...data,
      createName: user.nickName,
      createUserName: user.userName,
      updateName: user.nickName,
      updateUserName: user.userName,
    });
    databaseOperationLogList.push({
      type: 'add',
      dataType: 'obj',
      database: 'BaseColor',
      checkOnlyKeyList: [],
      data: color,
      whereObj: null,
      remark: `BaseColor表新增数据`,
    });
    await this.operationLogService.add(
      {
        type: 'add',
        msg: `水墨管理模块新增水墨`,
        databaseOperationLogList,
        remark: `新增水墨${data.colorName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 更新
   */
  async update(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const {
      id,
      color,
      colorName,
      colorModel,
      remark,
      colorTypeName,
      colorTypeCode,
      brand,
      viscosity,
      unitPrice,
    } = data;

    await this.colorRepository.update(
      { id },
      {
        color,
        colorName,
        colorModel,
        colorTypeCode,
        colorTypeName,
        brand,
        viscosity,
        unitPrice,
        remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      }
    );
    databaseOperationLogList.push({
      type: 'update',
      dataType: 'obj',
      database: 'BaseColor',
      checkOnlyKeyList: [],
      data: {
        color,
        colorName,
        colorModel,
        colorTypeCode,
        colorTypeName,
        brand,
        viscosity,
        unitPrice,
        remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      },
      whereObj: { id },
      remark: `BaseColor表更新水墨信息`,
    });
    await this.operationLogService.add(
      {
        type: 'update',
        msg: `水墨管理模块更新水墨信息`,
        databaseOperationLogList,
        remark: `更新水墨${data.colorName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 删除
   */
  async delete(idList: number[], user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    await this.colorRepository.delete(idList);
    databaseOperationLogList.push({
      type: 'delete',
      dataType: 'array',
      database: 'BaseColor',
      checkOnlyKeyList: [],
      data: null,
      whereObj: idList,
      remark: `BaseColor表删除水墨`,
    });
    await this.operationLogService.add(
      {
        type: 'delete',
        msg: `水墨管理模块删除水墨`,
        databaseOperationLogList,
        remark: `删除水墨`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  async isExistKey(data) {
    const { colorName, colorModel, colorCode } = data;
    const result = await this.colorRepository.findOne({ where: { colorName, colorModel } });

    const result1 = await this.colorRepository.findOne({ where: { colorCode } });

    if (result || result1) throw new ApiException(10049);
  }
}
